import { AiSample } from "@/types";
import { ReqImage, ResImage, ResImageCheck } from "@/types/aiImage";
import { aidrawClose, getAidrawCheck, getAidrawImage } from '@/api'
import { observable, action, makeObservable } from "mobx";
import { CollPromiseObj, createPromiseObj } from "@/utils/async";


class Active {
   /**参数 */
   @observable reqImage: ReqImage = {
      promt: "",
      style: "",
      format: "",
      promt_f: "",
      definition: "",
      is_synthesis: "",
      num: "",
      pic: "",
      pos: "",
      channel_id: ""
   }
   /**轮询 */
   @observable resImage: ResImage = {
      id: 0,
      jobId: "",
      jobId_f: "",
      createTime: 0
   }
   /**轮询 */
   @observable resCheck: ResImageCheck[] = []
   /**当前选中 */
   @observable currentCreatParam: AiSample = {
      id: 0,
      title: "",
      style: "",
      format: "",
      definition: "",
      prompt_m: "",
      prompt_f: "",
      preview: ""
   }
   @observable successStatus: 'none' | 'start' | 'finish' = 'none'

   OnePicSuccess: CollPromiseObj<ResImageCheck> | null = null
   SecPicsuccess: CollPromiseObj<ResImageCheck> | null = null
   pt = 'xcx'
   pollinghandle: null | NodeJS.Timeout = null

   constructor() {
      makeObservable(this)
   }

   @action getReqImage() {
      // 获取左上部分 

      return this.reqImage;
   }

   @action async createImage() {
      // 获取左上部分  
      if (this.reqImage.is_synthesis != '') {
         const res1 = await getAidrawImage(this.pt, this.reqImage)
         if (res1.code) {
            this.resImage = res1.data
            this.OnePicSuccess = createPromiseObj<ResImageCheck>()
            this.SecPicsuccess = createPromiseObj<ResImageCheck>()
         }

      }
   }
   /**
    * 生成图片轮询状态
    */
   @action async checkImageStatus() {
      // 获取左上部分 
      if (this.resImage.jobId != '') {
         const res1 = await getAidrawCheck({
            pt: this.pt,
            jobId: this.resImage.jobId
         })
         if (res1.code && res1.data.status === 'END') {

            if (this.OnePicSuccess) {
               this.OnePicSuccess.resolve(res1.data)
            }

         }
         const res2 = await getAidrawCheck({
            pt: this.pt,
            jobId: this.resImage.jobId_f
         })
         if (res2.code && res2.data.status === 'END') {
            if (this.SecPicsuccess) {
               this.SecPicsuccess.resolve(res2.data)
            }

         }
         if (this.resCheck.length > 1) {
            if (this.pollinghandle)
               clearInterval(this.pollinghandle)
         }
      } else {

      }
   }

   /**
    * 取消生成并开始轮询
    */
   @action async cannalPolling(successfn?: () => void, failfn?: () => void) {
      // 获取左上部分 
      if (this.resImage.jobId != '') {
         const res1 = await aidrawClose({
            jobId: this.resImage.jobId
         })
         const res2 = await aidrawClose({
            jobId: this.resImage.jobId_f
         })
         if (res1.code && res2.code) {
            this.OnePicSuccess = null
            this.SecPicsuccess = null
            if (this.pollinghandle)
               clearInterval(this.pollinghandle)
            this.successStatus = 'none'
            if (successfn) successfn()
         } else {
            if (failfn) failfn()
         }

      } else {
         this.OnePicSuccess = null
         this.SecPicsuccess = null
         if (this.pollinghandle)
            clearInterval(this.pollinghandle)
         this.successStatus = 'none'
         if (successfn) successfn()
      }


   }

   /**
    * 生成并开始轮询
    */
   @action async setCreateAndPolling(fn?: () => void, fail?: () => void) {
      try {
         this.successStatus = 'start'
         await this.createImage()
         this.pollinghandle = setInterval(async () => {
            await this.checkImageStatus()
         }, 2000)

         const one = await this.OnePicSuccess?.promiseObj
         const tow = await this.SecPicsuccess?.promiseObj
         if (one && tow) {
            this.resCheck = [one, tow];
            this.successStatus = 'finish'
            if (fn)
               fn()
         }
      }
      catch (e) {
         if (fail) {
            fail()
         }
      }
   }
   /**
    * 
    * @param param AiSample
    */
   @action initImageParam(param: AiSample) {
      // 获取左上部分 
      this.reqImage = {
         promt: param.prompt_m,
         promt_f: param.prompt_f,
         style: param.style,
         format: param.format,
         definition: param.definition,
         is_synthesis: '4',
         num: '1',
         pic: "",
         pos: "",
         channel_id: '2'
      }
      this.currentCreatParam = param
   }


}
export default Active;
